package view
{

import data.RadioEvents;

import flash.display.SimpleButton;
	import assets.radio.MainRadio;
	import com.TweenLite;
	import flash.events.*;
	import flash.display.MovieClip;
	import flash.geom.Rectangle;
	import flash.display.MovieClip
	
	/**
	 * Volume controll
	 * @author TrueTroll
	 */

	public class UIVolumeControll extends EventDispatcher
	{
		private var _visual:MovieClip;
		private var _volumeIndicators:Array = [];
		private var _volume:int = 0;
		public function UIVolumeControll(visual:MovieClip)
		{ 
			_visual = visual;
			_visual.bgBar.buttonMode = true;
			_volumeIndicators = [];
			for (var i:int = 0; i < _visual.numChildren ; i++) 
				if (_visual.getChildAt(i).name != "bgBar")
					_volumeIndicators.push(_visual.getChildAt(i));
			
			_visual.addEventListener(MouseEvent.MOUSE_DOWN, _onMouseDown);
			_visual.addEventListener(MouseEvent.MOUSE_MOVE, _onMouseMove);
			_visual.addEventListener(MouseEvent.MOUSE_UP, _onMouseUp);
			_visual.parent.addEventListener(MouseEvent.MOUSE_WHEEL, _onMouseWhell);
			_update();
		}
		
		private var _downFlag:Boolean = false;
		private function _onMouseDown(event:MouseEvent):void
		{
			_downFlag = true;
			var percent:Number = event.localX / _visual.bgBar.width;
			_volume = percent * 100;
			_update();
		}
		
		private function _onMouseWhell(event:MouseEvent):void
		{
			if(event.delta>0)
				_volume += 5;
			else
				_volume -= 5;
			_update();
		}
		
		private function _onMouseUp(event:MouseEvent):void
		{
			_downFlag = false;
		}
		
		private function _onMouseMove(event:MouseEvent):void
		{
			if (!_downFlag) return;
			var percent:Number = event.localX / _visual.bgBar.width;
			_volume = percent * 100;
			_update();
		}
		
		private function _update():void
		{
			_volume = Math.min(100, _volume);
			_volume = Math.max(0, _volume);
			for (var i:int = 0; i < _volumeIndicators.length ; i++) 
			{
				_volumeIndicators[i].gotoAndStop(1);
				if(_volume<(i*20)+5) 
					_volumeIndicators[i].gotoAndStop(2);
			}
			dispatchEvent(new Event(RadioEvents.VOLUME_CHANGED));
		}
		
		public function get value():Number
		{
			return _volume;
		}
		
		public function set value(v:Number):void
		{
			_volume = v;
			_update();
		}
		
		public function set muteMode(value:Boolean):void
		{
			var i:int = 0;
			if (value) {
				
				for (i = 0; i < _volumeIndicators.length ; i++) 
				{
					_volumeIndicators[i].gotoAndStop(2);
				}
				
			}else {
				for (i = 0; i < _volumeIndicators.length ; i++) 
				{
					_volumeIndicators[i].gotoAndStop(1);
					if(_volume<(i*20)+5) 
							_volumeIndicators[i].gotoAndStop(2);
				}
			}
			
			if(value) {
				this._visual.alpha = 0.8;
			} else{ 
				this._visual.alpha = 1;
			}
		}

	}
}